<?php
class PostController extends Zend_Controller_Action
{
	public function createAction()
	{
		if(null == ($identity = Blogger_Auth::getInstance()->getIdentity()))
			throw new Blogger_Exception('Authentication failure', Blogger_Exception_Codes::ERROR_AUTH_FAIL);
		$this->view->identity = $identity;
		$request = $this->getRequest();
		$createPostForm = new Application_Form_Post();
		if($request->isPost())
		{
			if($createPostForm->isValid($request->getPost()))
			{
				$posts = new Blogger_Posts();
				$posts->createPost($identity['id'], $createPostForm->getValue('text'), $createPostForm->getValue('title'));
				$this->_helper->flashMessenger->addMessage('Post published!');
				$this->_redirect('/dashboard/index');
				return;
			}
		}
		$this->view->createPostForm = $createPostForm;
	} 
	
	public function deleteAction()
	{
		if(null == ($identity = Blogger_Auth::getInstance()->getIdentity()))
			throw new Blogger_Exception('Authentication failure', Blogger_Exception_Codes::ERROR_AUTH_FAIL);
		$this->view->identity = $identity;
		$postId = $this->_getParam('id');
		if(empty($postId))
			throw new Blogger_Exception('Missing post id');
		$postsModel = new Blogger_Posts();
		if(false === $postsModel->hasPost($identity['id'], $postId))
			throw new Blogger_Exception("No post found for postId = $postId & userId = ".$identity['id']);
		$postsModel->deletePost($postId);
		$this->_helper->flashMessenger->addMessage('Post deleted!');
		$this->_redirect('/dashboard/index');
	}
	
	public function editAction()
	{
		if(null == ($identity = Blogger_Auth::getInstance()->getIdentity()))
			throw new Blogger_Exception('Authentication failure', Blogger_Exception_Codes::ERROR_AUTH_FAIL);
		$this->view->identity = $identity;
		$updatePostForm = new Application_Form_UpdatePost();
		$request = $this->getRequest();
		$postsModel = new Blogger_Posts();
		if($request->isPost()) {
			if($updatePostForm->isValid($request->getPost()))
			{
				$postId = $updatePostForm->getValue('id');
				if(false === $postsModel->hasPost($identity['id'], $postId))
					throw new Blogger_Exception("No post found for postId = $postId & userId = ".$identity['id']);
				$postsModel->updatePost($postId, $identity['id'], $updatePostForm->getValue('text'), $updatePostForm->getValue('title'));
				$this->_helper->flashMessenger->addMessage('Post updated!');
				$this->_redirect('/dashboard/index');
				return;
			}
		} 
		else
		{
			$postId = $this->_getParam('id');
			if(empty($postId))
				throw new Blogger_Exception('Missing post id');
			if(false === ($data = $postsModel->hasPost($identity['id'], $postId)))
				throw new Blogger_Exception("No post found for postId = $postId & userId = ".$identity['id']);
			$updatePostForm->getElement('text')->setValue($data['text']);
			$updatePostForm->getElement('title')->setValue($data['title']);
		}
		$idField = $updatePostForm->getElement('id');
		$idField->setValue($postId);
		$this->view->updatePostForm = $updatePostForm;
	}
	
	public function commentAction()
	{
		if(null == ($identity = Blogger_Auth::getInstance()->getIdentity()))
		{
			//TODO after loggin in user should be redirected to comments page
			$this->_helper->redirector('login', 'account');
			return;
		}
		$this->view->identity = $identity;
		$postCommentForm = new Application_Form_PostComment();
		$request = $this->getRequest();
		$commentsModel = new Blogger_Posts_Comments();
		if($request->isPost())
		{
			if($postCommentForm->isValid($request->getPost()))
			{
				$postId = $postCommentForm->getValue('id');
				$postsModel = new Blogger_Posts();
				if(null == ($postData = $postsModel->getPostById($postId)))
					throw new Blogger_Exception("No post found for post id = $postId");
				$commentsModel->postComment($postId, $postCommentForm->getValue('text'), $identity['id']);
				$this->_helper->flashMessenger->addMessage('Comment saved!');
				$users = new Blogger_Users();
				$userData = $users->getUserById($postData['userId']);
				//go back to blog page
				$this->_redirect('/blog/'.$userData['alias']);
				return;
			}
		}
		else
		{
			$postId = $this->_getParam('id');
			if(empty($postId))
				throw new Blogger_Exception('Missing post id');
		}
		$idField = $postCommentForm->getElement('id');
		$idField->setValue($postId);
		$this->view->postCommentForm = $postCommentForm;
	}
}